package com.uplift.mgt.sys.controller;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.uplift.common.Const;

/**
 * 通用下载接口
 * 
 * @author bcwang
 *
 */
@Controller
public class DownloadController {

	@RequestMapping("/download")
	public ResponseEntity<byte[]> download(String fileName, String filePath) throws IOException {
		File file = new File(Const.DIR_UPLOAD_BASE, filePath);
		if (StringUtils.isBlank(fileName)) {
			fileName = "download_file";
		} else {
			fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
		}
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", fileName);

		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
	}

}
